package com.ruoyi.assistant.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.assistant.domain.Course;
import com.ruoyi.assistant.domain.Exam;
import com.ruoyi.assistant.dto.Answer;
import com.ruoyi.assistant.dto.ExamAnswerDto;
import com.ruoyi.assistant.dto.ExamDto;
import com.ruoyi.assistant.mapper.CourseMapper;
import com.ruoyi.assistant.mapper.ExamMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.assistant.mapper.QuestionMapper;
import com.ruoyi.assistant.domain.Question;
import com.ruoyi.assistant.service.IQuestionService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 试题管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private CourseMapper courseMapper;

    /**
     * 查询试题管理
     *
     * @param id 试题管理主键
     * @return 试题管理
     */
    @Override
    public Question selectQuestionById(Long id) {
        return questionMapper.selectQuestionById(id);
    }

    /**
     * 查询试题管理列表
     *
     * @param question 试题管理
     * @return 试题管理
     */
    @Override
    public List<Question> selectQuestionList(Question question) {
        return questionMapper.selectQuestionList(question);
    }

    /**
     * 新增试题管理
     *
     * @param question 试题管理
     * @return 结果
     */
    @Override
    public int insertQuestion(Question question) {
        return questionMapper.insertQuestion(question);
    }

    /**
     * 修改试题管理
     *
     * @param question 试题管理
     * @return 结果
     */
    @Override
    public int updateQuestion(Question question) {
        return questionMapper.updateQuestion(question);
    }

    /**
     * 批量删除试题管理
     *
     * @param ids 需要删除的试题管理主键
     * @return 结果
     */
    @Override
    public int deleteQuestionByIds(String ids) {
        return questionMapper.deleteQuestionByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除试题管理信息
     *
     * @param id 试题管理主键
     * @return 结果
     */
    @Override
    public int deleteQuestionById(Long id) {
        return questionMapper.deleteQuestionById(id);
    }

    @Override
    @Transactional
    public AjaxResult saveQuestions(ExamDto examDto) {
        Course course = examDto.getCourse();

        List<Course> courses = courseMapper.selectCourseList(course);
        if (courses.size() == 0) {
            int i = courseMapper.insertCourse(course);
            if (i <= 0) {
                return AjaxResult.error("课程插入失败");
            }
        }

        String examName = examDto.getExamName();
        Exam exam = new Exam();
        exam.setCourseId(String.valueOf(course.getId()));
        exam.setCourseName(course.getName());
        exam.setExamName(examName);
        List<Exam> examList = examMapper.selectExamList(exam);
        if (examList.size() == 0) {
            int insertExam = examMapper.insertExam(exam);
            if (insertExam <= 0) {
                return AjaxResult.error("考试插入失败");
            } else {
                examDto.getQuestionList().forEach(questionDto -> {
                    Question question = new Question();
                    String questionName = questionDto.getQuestion();
                    question.setQuestion(questionName);
                    question.setExamId(exam.getId());
                    String[] split = questionName.split("\\.");
                    question.setQuestionNum(Long.valueOf(split[0]));
                    questionMapper.insertQuestion(question);
                });
            }
        } else {
            return AjaxResult.error("考试已经存在，请不要重复导入。");
        }

        return AjaxResult.success("题目插入成功");
    }

    @Override
    public AjaxResult fillQuestions(ExamDto examDto) {
        Long courseId = examDto.getCourse().getId();
        String examName = examDto.getExamName();
        Exam exam = new Exam();
        exam.setCourseId(String.valueOf(courseId));
        exam.setExamName(examName);
        Long examId = examMapper.getExamIdByCourseId(courseId);
        List<Answer> answerList = examDto.getQuestionList().stream().map(questionDto -> {
            Question question = new Question();
            question.setQuestion(questionDto.getQuestion());
            question.setExamId(examId);
            List<Question> questions = questionMapper.selectQuestionList(question);
            if (questions != null && questions.size() > 0) {
                Question que = questions.get(0);
                Answer answer = new Answer(que.getQuestion(), que.getCorrectAnswer());
                return answer;
            }
            return null;
        }).collect(Collectors.toList());
        return AjaxResult.success("回填数据成功", answerList);
    }

    @Override
    public AjaxResult matchingAnswers(List<ExamAnswerDto> examAnswerDtos) {
        ExamAnswerDto examAnswerDtosFirst = examAnswerDtos.get(0);
        String courseName = examAnswerDtosFirst.getCourseName().trim();
        String examName = examAnswerDtosFirst.getExamName().trim();
        Long examId = examMapper.getIdByExamNameAndCourseName(examName,courseName);
        if (examId != null && examId != 0) {
            examAnswerDtos.stream().forEach(examAnswerDto -> {
                Question question = new Question();
                question.setExamId(examId);
                question.setQuestionNum(examAnswerDto.getQuestionNum());
                question.setCorrectAnswer(examAnswerDto.getCorrectAnswer().trim());
                questionMapper.updateQuestionByExamIdAndQuestionNum(question);
            });
        }else {
            return AjaxResult.error("考试不存在");
        }
        return AjaxResult.success("题目答案匹配成功");
    }
}
